package com.spddr.pmos.rpt.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Period;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.spddr.pmos.repos.HydrologyStationRepos;
import com.spddr.pmos.rpt.RptHydrologyStation;
import com.spddr.pmos.rpt.pojo.CellValue;

import cn.seqdata.echarts.Option;
import cn.seqdata.echarts.axis.CategoryAxis;
import cn.seqdata.echarts.axis.ValueAxis;
import cn.seqdata.echarts.series.Bar;
import cn.seqdata.tags.Point;
import cn.seqdata.tags.common.IdTags;

@Service
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class RptHydrologyStationImpl extends AbstractPivotChartImpl implements RptHydrologyStation {

	@Autowired
	private HydrologyStationRepos hydrologyStationRepos;

	protected Period intervalOfStorage() {
		return Days.ONE.equals(interval) ? Hours.ONE.toPeriod() : Days.ONE.toPeriod();
	}

	@Override
	public List<CellValue> loadPivot() {
		return hydrologyStationRepos.findAll().stream().flatMap(s -> {
			// RegularValues<IdTags, Point<IdTags>> r =
			// load(TreeLevel.Hydrology.id, s.getId(), Metrics.FLOW_IN.name);
			Point<IdTags> point = new Point<>(metric, new IdTags(id));
			return sim(1).getValue().getValues().stream().map(v -> new CellValue(point, v, null, s.getName()));
		}).collect(Collectors.toList());
	}

	@Override
	public Option chart() {
		Option option = new Option();

		CategoryAxis xAxis = new CategoryAxis().show(true);
		xAxis.axisLabel().rotate(45);
		xAxis.axisLabel().interval(0);
		option.xAxis(xAxis);

		option.yAxis(new ValueAxis().show(true));

		Bar bar = new Bar();
		option.series().add(bar);

		hydrologyStationRepos.findAll().stream().forEach(s -> {
			xAxis.data().add(s.getName());
			bar.data().add(String.format("%.2f", Math.random() * 100));
		});

		return option;
	}

}
